package com.hibase.core.rule.web.ruletable;

import com.github.pagehelper.PageInfo;
import com.hibase.common.entity.ResponseModel;
import com.hibase.common.util.EntityUtil;
import com.hibase.core.rule.constant.RuleStatus;
import com.hibase.core.rule.entity.ruletable.RuleTable;
import com.hibase.core.rule.entity.ruletable.vo.*;
import com.hibase.core.rule.service.ruletable.RuleTableService;
import com.hibase.core.web.annotation.HibasePostRequestMapping;
import com.hibase.core.web.entity.OperationIdEntity;
import com.hibase.core.web.entity.OperationIdsEntity;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 动态规则表 前端控制器
 * </p>
 *
 * @author hufeng
 * @since 2019-06-10
 */
@RestController
@RequestMapping("/rule-table")
@Api(tags = "动态规则表")
public class RuleTableController{

    @Autowired
    private RuleTableService ruleTableService;

    @ApiOperation(value = "查询动态规则表列表", httpMethod = "POST")
    @HibasePostRequestMapping(value = "selectRuleTableList")
    public ResponseModel<RuleTableListVO> selectRuleTableList(@RequestBody RuleTableSearchVO searchVO) {

        PageInfo result = ruleTableService.selectRuleTableList(searchVO);

        result.setList(EntityUtil.transform(result.getList(), RuleTableListVO.class));

        return ResponseModel.successMsg(result);
    }

    @ApiOperation(value = "新增动态规则表记录", httpMethod = "POST")
    @HibasePostRequestMapping(value = "insertRuleTable")
    public ResponseModel<String> insertRuleTable(@RequestBody RuleTableInsertVO insertVO) {

        RuleTable entity = EntityUtil.transform(insertVO, RuleTable.class);

        ruleTableService.saveRuleTable(entity);

        return ResponseModel.successMsg(entity.getId());
    }

    @ApiOperation(value = "编辑动态规则表+表字段", httpMethod = "POST")
    @HibasePostRequestMapping(value = "updateRuleTable")
    public ResponseModel<String> updateRuleTable(@RequestBody RuleTableUpdateVO updateVO) {

        ruleTableService.updateRuleTable(updateVO);

        return ResponseModel.successMsg(updateVO.getId());
    }

    @ApiOperation(value = "查询动态规则表详情+表字段", httpMethod = "POST")
    @HibasePostRequestMapping(value = "viewRuleTableDetails")
    public ResponseModel<RuleTableViewVO> viewRuleTableDetails(@RequestBody OperationIdEntity operationidEntity) {

        return ResponseModel.successMsg(ruleTableService.viewRuleTable(operationidEntity.getId()));
    }

    @ApiOperation(value = "删除动态规则表记录", httpMethod = "POST")
    @HibasePostRequestMapping(value = "/deleteRuleTable")
    public ResponseModel<String> deleteRuleTable(@RequestBody OperationIdsEntity delete) {

        // todo, 删除后 ，是否可以添加数据？
        ruleTableService.deleteRuleTable(delete.getIds());

        return ResponseModel.successMsg();
    }

    @ApiOperation(value = "启用动态规则表", httpMethod = "POST")
    @HibasePostRequestMapping(value = "/enableRuleTable")
    public ResponseModel<String> enableRuleTable(@RequestBody OperationIdsEntity idEntity) {

        ruleTableService.operaRuleTable(idEntity.getIds(), RuleStatus.RULE_STATUS_RELEASE);

        return ResponseModel.successMsg();
    }

    @ApiOperation(value = "禁用动态规则表", httpMethod = "POST")
    @HibasePostRequestMapping(value = "/disableRuleTable")
    public ResponseModel<String> disableRuleTable(@RequestBody OperationIdsEntity idEntity) {

        ruleTableService.operaRuleTable(idEntity.getIds(),RuleStatus.RULE_STATUS_DISABLE);

        return ResponseModel.successMsg();
    }
    @ApiOperation(value = "导入动态规则表记录", httpMethod = "POST")
    @HibasePostRequestMapping(value = "/importRuleTableVersion")
    public ResponseModel<String> importRuleTableVersion(@RequestBody OperationIdEntity delete) {

        //ruleTableVersionService.deleteRuleTableVersion(delete.getId());

        return ResponseModel.successMsg();
    }


}
